/**
 * Description:
 * Class Name:JoinThread
 * Date:2022/5/5 16:07
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第16章_多线程._16_4_控制线程._16_4_1_join线程;

public class JoinThread extends Thread{
    public JoinThread(String name){
        super(name);
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(getName() + "_" + i);
        }
    }

    public static void main(String[] args) throws Exception{
        new JoinThread("新线程").start();
        for (int i = 0; i < 100; i++) {
            if (i == 20){
                var jt = new JoinThread("被Join的线程");
                jt.start();
                // main线程调用了jt线程的join()方法，main线程必须等待jt执行结束才会向下执行
                jt.join();
            }
            System.out.println(Thread.currentThread().getName() + "_" + i);
        }
    }
}
// ...
//被Join的线程_98
//被Join的线程_99
//main_20
//main_21
//main_22
//main_23
//main_24
//main_25
// ...